//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import React, { useEffect, useState } from "react";
import { observer } from 'mobx-react';
import { Form, Input, message, Modal, Select } from "antd";
import { useStores } from "@/hooks";
import type { AssetCateInfo } from "@/api/org_asset";
import { add_asset, list_asset_cate, ASSET_OWNER_DEPART_MENT, ASSET_OWNER_MEMBER } from "@/api/org_asset";
import { request } from "@/utils/request";

export interface AddAssetModalProps {
    departMentId: string;
    memberUserId?: string;
    onCancel: () => void;
    onOk: () => void;
}

const AddAssetModal = (props: AddAssetModalProps) => {
    const userStore = useStores("userStore");
    const orgStore = useStores("orgStore");

    const [memberUserId, setMemberUserId] = useState(props.memberUserId ?? "");
    const [cateInfoList, setCateInfoList] = useState<AssetCateInfo[]>([]);
    const [curCateId, setCurCateId] = useState("");
    const [assetNo, setAssetNo] = useState("");
    const [assetDesc, setAssetDesc] = useState("");

    const loadCateInfoList = async () => {
        const res = await request(list_asset_cate({
            session_id: userStore.sessionId,
            org_id: orgStore.curOrgId,
        }));
        setCateInfoList(res.cate_list);
        if (res.cate_list.length > 0 && curCateId == "") {
            setCurCateId(res.cate_list[0].asset_cate_id);
        }
    };

    const addAsset = async () => {
        await request(add_asset({
            session_id: userStore.sessionId,
            org_id: orgStore.curOrgId,
            asset_cate_id: curCateId,
            asset_no: assetNo,
            asset_desc: assetDesc,
            owner_type: memberUserId == "" ? ASSET_OWNER_DEPART_MENT : ASSET_OWNER_MEMBER,
            owner_id: memberUserId == "" ? props.departMentId : memberUserId,
        }));
        message.info("增加成功");
        props.onOk();
    };

    useEffect(() => {
        loadCateInfoList();
    }, []);

    return (
        <Modal open title="增加资产" mask={false}
            okText="增加" okButtonProps={{ disabled: curCateId == "" }}
            onCancel={e => {
                e.stopPropagation();
                e.preventDefault();
                props.onCancel();
            }}
            onOk={e => {
                e.stopPropagation();
                e.preventDefault();
                addAsset();
            }}>
            <Form>
                {props.memberUserId == undefined && (
                    <Form.Item label="资产归属">
                        <Select value={memberUserId} onChange={value => setMemberUserId(value)}>
                            <Select.Option value="">公共资产</Select.Option>
                            {orgStore.memberList.filter(item => item.parent_depart_ment_id == props.departMentId).map(item => (
                                <Select.Option key={item.member_user_id} value={item.member_user_id}>{item.display_name}</Select.Option>
                            ))}
                        </Select>
                    </Form.Item>
                )}
                <Form.Item label="资产类别">
                    <Select value={curCateId} onChange={value => setCurCateId(value)}>
                        {cateInfoList.map(item => (
                            <Select.Option key={item.asset_cate_id} value={item.asset_cate_id}>{item.cate_name}</Select.Option>
                        ))}
                    </Select>
                </Form.Item>
                <Form.Item label="资产编号">
                    <Input value={assetNo} onChange={e => {
                        e.stopPropagation();
                        e.preventDefault();
                        setAssetNo(e.target.value.trim());
                    }} placeholder="可选" />
                </Form.Item>
                <Form.Item label="资产备注">
                    <Input.TextArea autoSize={{ minRows: 3, maxRows: 3 }} value={assetDesc} onChange={e => {
                        e.stopPropagation();
                        e.preventDefault();
                        setAssetDesc(e.target.value);
                    }} placeholder="可选" />
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default observer(AddAssetModal);